<template>
  <!-- 发送短信弹窗 -->
  <el-dialog title="发送短信" :visible.sync="dialogVisible" @close="close" :close-on-click-modal="false" width="850px" append-to-body>
    <div class="form-box">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="卡位手机号" v-if="tel">
          <div>{{ tel }}</div>
        </el-form-item>
        <el-form-item label="运营商" prop="gameCarrierId">
          <el-select v-model="form.gameCarrierId" placeholder="请选择游戏游戏运营商" @change="carrierChange">
            <el-option v-for="dict in yunyingshangList" :key="dict.id" :label="dict.name" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="接收号码" prop="toNum">
          <el-select v-model="form.toNum" placeholder="先选择运营商">
            <el-option v-for="dict in numbersList" :key="dict.id" :label="dict.number+'('+dict.remark+')'" :value="dict.number" />
          </el-select>
        </el-form-item>
        <el-row>
          <el-form-item label="短信内容" prop="content">
            <el-input v-model="form.content" type="textarea" placeholder="请填写短信内容" :rows="3" style="width:600px" />
          </el-form-item>
        </el-row>

        <div class="center">
          <el-button @click="close()">取消</el-button>
          <el-button type="primary" @click="submitForm('form')" v-loading="loading">发送</el-button>
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import phoneApi from "@/api/phoneManage";
import gameListApi from "@/api/gameConfig/gameList";
import { decryke } from "../../../../../utils/jsencrypt";
export default {
  name: "",
  data() {
    return {
      dialogVisible: false,
      loading: false,
      numbersList: [],
      yunyingshangList: [],
      // 表单参数
      form: {
        telManageId: "",
        gameCarrierId: "",
        gameCarrierName:'',
        toNum: "",
        content: "",
      },
      rules: {
        gameCarrierId: [
          { required: true, message: "请选择运营商", trigger: "change" },
        ],
        toNum: [
          { required: true, message: "请选择接收号码", trigger: "change" },
        ],
        content: [
          { required: true, message: "请输入短信内容", trigger: "blur" },
        ],
      },
      tel:''
    };
  },
  created() {},
  methods: {
    // 打开弹窗
    open(data, flag) {
      console.log("%c Line:66 🥝 data", "color:#33a5ff", data);
      if (flag == "zude") {
        // 租的卡位
        this.yunyingshangList = data.carrierTypeVoList;
        this.form.telManageId = data.telManageId;
      } else {
        // 自己的卡位
        this.yunyingshangList = this.$store.getters.gameYunyingshangList;
        this.form.telManageId = data.id;
      }
      this.tel = ''
      if(data.tel) this.tel = data.tel
      this.dialogVisible = true;
    },
    // 获取运营商绑定的号码列表 getGameCarrierNumberList
    getGameCarrierNumberList(id) {
      gameListApi
        .getGameCarrierNumberList({
          gameCarrierId: id,
        })
        .then((res) => {
          this.numbersList = res.rows;
        });
    },
    // 选择运营商
    carrierChange(id) {
      this.yunyingshangList.forEach(r=>{
        if(r.id==id){
          this.form.gameCarrierName = r.name
        }
      })
      this.getGameCarrierNumberList(id);
    },

    // 重置
    reset() {
      this.$refs.form.resetFields();
    },
    // /** 查询详情进行回显 */
    // handleUpdate(row) {
    //   this.form = row;
    // },
    submitForm(formName) {
      this.$refs["" + formName].validate((valid) => {
        if (valid) {
          this.$confirm("确定发送短信吗？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "",
          })
            .then(() => {
              this.loading = true
              // 处理参数
              const timestamp = new Date().getTime().toString()
              const str = JSON.stringify({
                ...this.form,
                timestamp
              })
              const sign = this.encryptAESAndMD5(str,decryke)
              const headers = {
                sign:sign,
                timestamp:timestamp
              }
              phoneApi.telSendSms(this.form,headers).then((res) => {
                this.loading = false
                this.$message.success(res.msg);
                this.close();
              }).catch(err=>{
                if(err=='数据错误'||err=='参数错误'){
                  this.$confirm(
                    "参数错误，点击确定刷新页面重试！",
                    "提示",
                    {
                      confirmButtonText: "确定",
                      cancelButtonText: "取消",
                      type: "warning",
                    }
                  )
                    .then(() => {
                      location.reload(true); // 强制从服务器重新加载
                    })
                    .catch(() => {
                      
                    });
                }
                // this.$message.error(err.msg);
                this.loading = false
              })
            })
            .catch(() => {});
        } else {
          this.$alert("请填写完整！");
        }
      });
    },

    close() {
      this.reset();
      this.dialogVisible = false;
    },

  
  },
};
</script>
<style scoped lang="css">
.center {
  text-align: center;
}
.form-box {
  padding-top: 20px;
}
</style>
